#include <bits/stdc++.h>
#define ll long long
#define pr printf
#define sc scanf
using namespace std;
int main()
{
    int n;
    cin >> n;
    string test;
    int l = 0;
    for (int i = 0; i < n; i++)
    {
        int left = 0, mid = 0, right = 0, t = 0, p = 0;
        cin >> test;
        l = test.size();
        for (int i = 0; i < l; i++)
        {
            if (test[i] == 'A' && p == 0 && t == 0)
            {
                left++;
                continue;
            }
            if (test[i] == 'P')
            {
                p++;
                continue;
            }
            if (test[i] == 'A' && p == 1 && t == 0)
            {
                mid++;
                continue;
            }
            if (test[i] == 'T' && mid >= 1)
            {
                t++;
                continue;
            }
            if (test[i] == 'A' && p == 1 && t == 1)
            {
                right++;
                continue;
            }
            else
            {
                break;
            }
        }
        if (p == 1 && t == 1 && left * mid == right)
        {
            cout << "YES" << endl;
        }
        else
        {
            cout << "NO" << endl;
        }
    }
    return 0;
}
